<?php

namespace App\Http\Resources\CRM;

use App\Enums\OrderItemType;
use App\Enums\TableStatus;
use App\Models\Order;
use App\Models\OrderItem;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\DB;

class TableListResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        /**
         * @var Order $order
         */
        $order = $this->tableOrder;
        $assistantTotalMinutes = 0;
        $assistantList = [];
        if (!empty($order)) {
            $assistantItems = $order->items()->where('type', OrderItemType::Assistant)->get();
            foreach ($assistantItems as $item) {
                $assistantTotalMinutes += $item->getTotalMinutes(date('Y-m-d H:i:s'));
                $assistantList[] = $item->assistantUser;
            }
        }

        $query = OrderItem::where('table_id', $this->id)
            ->whereBetween('created_at', [
                date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')
            ]);

        $tableItems = $query->whereNotNull('end_at')->get();
        $orderIds = $query->pluck('order_id')->toArray();
        $dayTableTotalMoney = $query->sum('money');
        $dayTableTotalMinutes = 0;
        foreach ($tableItems as $item) {
            $dayTableTotalMinutes += $item->getTotalMinutes(date('Y-m-d H:i:s'));
        }
        $dayAssistantList = [];
        $assistantItems = OrderItem::whereIn('order_id', $orderIds)
            ->where('type', OrderItemType::Assistant)
            ->with(['assistantUser'])
            ->get();
        foreach ($assistantItems as $item) {
            $dayAssistantList[] = $item->assistantUser;
        }

        $data = [
            'id' => $this->id,
            'sort' => $this->sort,
            'name' => $this->name,
            'people_count' => $this->people_count,
            'type_id' => $this->type_id,
            'status' => $this->status,
            'use_start_at' => $this->use_start_at,
            'table_order_id' => $this->table_order_id,
            'status_label' => $this->status->label(),
            'order' => $this->tableOrder,
            'current' => [
                'table_total_money' => empty($order) ? null : $this->getTableTotalMoney(date('Y-m-d H:i:s')),
                'table_total_minutes' => empty($order) ? null : $this->getTableTotalMinutes(date('Y-m-d H:i:s')),
                'goods_total_money' => empty($order) ? null : $order->items()->where('type', OrderItemType::Goods)->sum('money'),
                'goods_total_count' => empty($order) ? null : $order->items()->where('type', OrderItemType::Goods)->count(),
                'assistant_total_money' => empty($order) ? null : $order->items()->where('type', OrderItemType::Assistant)->sum('money'),
                'assistant_total_minutes' => $assistantTotalMinutes,
                'assistant_list' => $assistantList,
            ],
            'day' => [
                'table_total_money' => $dayTableTotalMoney,
                'table_total_minutes' => $dayTableTotalMinutes,
                'goods_total_money' => OrderItem::whereIn('order_id', $orderIds)
                    ->where('type', OrderItemType::Goods)
                    ->sum('money'),
                'goods_total_count' => OrderItem::whereIn('order_id', $orderIds)
                    ->where('type', OrderItemType::Goods)
                    ->count(),
                'assistant_total_money' => OrderItem::whereIn('order_id', $orderIds)
                    ->where('type', OrderItemType::Assistant)
                    ->sum('money'),
                'assistant_total_minutes' => OrderItem::whereIn('order_id', $orderIds)
                    ->where('type', OrderItemType::Assistant)
                    ->sum('money'),
                'assistant_list' => $dayAssistantList,
                'current_member' => [], //TODO: 当前客户
                'book_member' => null,  //TODO: 预定客户
                'book_assistant' => null,   //TODO: 预定助教
                'member_count' => Order::whereIn('id', $orderIds)->whereNotNull('member_id')->count(),
                'order_count' => Order::whereIn('id', $orderIds)->count(),
            ]
        ];

        return $data;
    }
}
